<template>
  <el-dialog
    width="630px"
    title="工单配置"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    v-model="visible"
    append-to-body
    @close="cancel"
    @open="open"
  >
    <el-form
      ref="taskRef"
      :inline="true"
      :model="form"
      :rules="rules"
      label-width="120"
    >
      <el-form-item label="补货警戒线：" prop="alertValue">
        <el-input-number
          v-model="form.alertValue"
          controls-position="right"
          :min="1"
          :max="100"
          placeholder="请输入"
        />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="cancel"> 取消 </el-button>
      <el-button type="primary" @click="submitForm"> 确认 </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getTask, listTask } from '@/api/manager/task';

export default {
  name: 'Task',
  props: {
    // 弹层显示隐藏
    taskConfigVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      form: {
        id: null,
        alertValue: null
      },
      rules: {
        alertValue: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    taskConfigVisible(val) {
      if (val) {
        this.visible = val;
        this.open();
      }
    }
  },
  methods: {
    // 打开弹层
    open() {
      this.getJobData();
    },

    // 获取补货预警值
    // getJobData() {
    //   getJob(1).then(response => {
    //     const res = response.data;
    //     this.form.id = res.id;
    //     this.form.alertValue = res.alertValue;
    //   });
    // },

    // 提交表单
    submitForm() {
      this.$refs['taskRef'].validate(valid => {
        if (valid) {
          setJob(this.form).then(res => {
            if (res.code === 200) {
              this.$modal.msgSuccess('配置成功');
              this.cancel();
              this.getJobData();
            }
          });
        }
      });
    },

    // 关闭弹层
    cancel() {
      this.visible = false;
      this.$emit('handleClose');
    }
  }
};
</script>
